package com.delivery.deliverysystem.controller;

import com.delivery.deliverysystem.pojo.DAO.ProductDAO;
import com.delivery.deliverysystem.pojo.DO.Product;
import com.delivery.deliverysystem.pojo.DO.ProductCategory;
import com.delivery.deliverysystem.pojo.VO.ProductNameVO;
import com.delivery.deliverysystem.pojo.VO.ProductVO;
import com.delivery.deliverysystem.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.delivery.deliverysystem.common.ServerResponse;

import java.util.List;

/**
 * 产品类别控制类
 */
@RestController
@RequestMapping("/product")
@Api(tags = "产品资料接口")
public class ProductController {
    @Autowired
    private ProductService productService;

    @PostMapping("/createProdct")
    @ApiOperation(value = "新增产品资料", response = ServerResponse.class, httpMethod = "POST")
    public ServerResponse createProdct(@RequestParam("file") MultipartFile file, ProductDAO productDAO){
        return ServerResponse.createBySuccess(productService.createProdct(file,productDAO));
    }
    @PostMapping("/updateProdct")
    @ApiOperation(value = "修改产品资料", response = ServerResponse.class, httpMethod = "POST")
    public ServerResponse updateProdct(@RequestParam("file") MultipartFile file, ProductDAO productDAO){
        return ServerResponse.createBySuccess(productService.updateProdct(file,productDAO));
    }
    @PostMapping("/deleteProduct")
    @ApiOperation(value = "删除产品资料",response = ServerResponse.class,httpMethod = "POST")
    public ServerResponse deleteProduct(@RequestParam String id){
        boolean b = productService.removeById(id);
        if(b) return ServerResponse.createBySuccess("删除成功");
        return ServerResponse.createByError("删除失败");
    }
    @GetMapping("/getProductById")
    @ApiOperation(value = "根据id查询产品资料",response = ServerResponse.class,httpMethod = "GET")
    public ServerResponse getProductById(@RequestParam String id){
        Product product = productService.getProductById(id);
        if(product==null) return ServerResponse.createByError("产品不存在");
        return ServerResponse.createBySuccess(product);
    }
    @GetMapping("/getProductList")
    @ApiOperation(value = "获取全部产品资料",response = ServerResponse.class,httpMethod = "GET")
    public ServerResponse getProductList(){
        List<ProductVO> productList = productService.getProductList();
        if(productList!=null && !productList.isEmpty()) return ServerResponse.createBySuccess(productList);
        return ServerResponse.createBySuccess("产品类别列表为空");
    }
    @GetMapping("/getProductStatusList")
    @ApiOperation(value = "获取全部启用的产品资料",response = ServerResponse.class,httpMethod = "GET")
    public ServerResponse getProductStatusList(){
        List<ProductNameVO> productList = productService.getProductStatusList();
        if(productList!=null && !productList.isEmpty()) return ServerResponse.createBySuccess(productList);
        return ServerResponse.createBySuccess("产品类别列表为空");
    }

}
